import org.junit.Test;

import java.security.Key;
import java.sql.SQLOutput;
import java.util.*;

public class TestCpllection {

    //测试collection接口的使用
    @Test
    public void Clollection(){
        Collection<Integer> c=new ArrayList();
        c.add(100);
        c.add(100);
        c.add(100);
        c.add(100);
        c.add(100);
        c.add(100);
        System.out.println(c);
        System.out.println(c.toArray());
        System.out.println(c.equals(200));
        System.out.println(c.contains(100));
        System.out.println(c.size());
        Iterator it=c.iterator();
        while (it.hasNext()){
            Object next = it.next();
            System.out.println(next);
        }

    }

//list的集合测试
    @Test
    public void ListTest(){
        List<String> list=new ArrayList<>();
        list.add("大力娃");//向list集合中存入数据
        list.add("千顺娃");
        list.add("头铁娃");
        list.add("喷火娃");
        list.add("喷水娃");
        list.add("隐身娃");
        list.add("小紫娃");
      //  System.out.println(list);
        System.out.println(list.contains("大力娃"));
        System.out.println(list.size());

        System.out.println(list.get(2));
        list.set(6, "你妹的");
        System.out.println(list);
        //将集合变成数组
        System.out.println(Arrays.toString(list.toArray()));
    }


    //集合list的数据查询方式
    @Test
    public void List1(){
        List<String> list=new ArrayList();
        //2.向创建好list集合添加元素
        list.add("喜羊羊");
        list.add("美羊羊");
        list.add("懒羊羊");
        list.add("沸羊羊");
        list.add("小肥羊");
        list.add("肥羊卷");

        System.out.println(list);
        //第一种方式for
        for (int i = 0; i <list.size() ; i++) {
            System.out.print(list.get(i)+"  ");

        }
        System.out.println();
        //增强for循环
        for (String s:list) {
            System.out.print(s+" ");
        }
        System.out.println();
        //第三种方式，集合迭代器
        ListIterator<String> it2=list.listIterator();
        while (it2.hasNext()){
            System.out.print(it2.next()+"  ");
        }


    }

    //map集合的基本的操作
    @Test
    public void map1(){
    Map<Integer,String> map=new HashMap<>();
        map.put(1,"白骨精");
        map.put(2,"黑熊精");
        map.put(3,"鲤鱼精");
        map.put(4,"黄毛怪");
        map.put(5,"黑熊精");
        map.put(6,"女儿国国王");
        System.out.println(map);
        System.out.println(map.hashCode());
        System.out.println(map.equals("白骨精"));
        System.out.println(map.isEmpty());
        System.out.println(map.size());
        System.out.println(map.containsKey(1));
        System.out.println(map.containsValue("白骨精"));

        //map的集合的跌带方式
        //因为没有map迭代器，所有需要先转成set集合，调用里面的方法
        Iterator<Integer> it=map.keySet().iterator();
        while (it.hasNext()){
            Integer key=it.next();
           String value=map.get(key);
            System.out.println(key+":"+value);
        }


    }

    //set的集合的操作
    @Test
    public void TestSet(){
        //1.创建对象
        //Set s = new Set();//父接口不可直接创建对象
        Set<String> set = new HashSet<String>();
        //2.存入数据
        set.add("牛气冲天");
        set.add("虎虎生威");
        set.add("虎虎生威");
        set.add(null);//向集合中存入null值
        set.add(null);
        set.add("null");//向集合中存入字符串null
        System.out.println(set);
  Set<Student> set1=new HashSet<>();
  set1.add(new Student("张三",15));
  set1.add(new Student("张四",15));
  set1.add(new Student("张无",15));
        //System.out.println(set1);
        Iterator<Student> it=set1.iterator();
        while (it.hasNext()){
            Student s = it.next();
            System.out.println(s);
           // System.out.println(2<<4);
        }


    }
    //创建学生类做测试
    class Student{
       private String name;
       private Integer age;
        public Student(){}

        public Student(String name,Integer age) {
            this.name = name;
            this.age=age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }

}
